package com.bwhx.eruler.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bwhx.eruler.bean.ChuKuDan;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Ruler;
import com.bwhx.eruler.bean.RulerUserER;
import com.bwhx.eruler.dao.RulerDao;

/**
 * @Description:
 */
@Repository
public class RulerDaoImpl extends BaseDaoImpl<Ruler> implements RulerDao {
	public RulerDaoImpl() {
		this.setNs("com.bwhx.eruler.mapper.RulerMapper.");
	}


	public List<Ruler> findByName(String rulername) {
	
		return getSqlSession().selectList("com.bwhx.eruler.mapper.RulerMapper." + "findByName", rulername);
	}


	@Override
	public synchronized String insertSN(String device) {
		Object selectOne = getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "getMaxRSNINT",device);
		Integer rsnInt = -1;
		String rsn ="-1";
		if(null!=selectOne && selectOne instanceof Integer){
			rsnInt = (Integer)selectOne+1;
			rsn = rsnInt2Rsn(rsnInt,device);
		}else{
			if(device.equals("1")){
				rsn="AA000001";
				rsnInt=1;
			}else if(device.equals("2")){
				rsn="ER000001";
				rsnInt=1;
			}else if(device.equals("3")){
				rsn="EB000001";
				rsnInt=1;
			}else if(device.equals("4")){
				rsn="ES000001";
				rsnInt=1;
			}else if(device.equals("5")){
				rsn="EA000001";
				rsnInt=1;
			}
			
		}
		
		if(!"-1".equals(rsn)){
			HashMap<String,Object> hashMap = new HashMap<>();
			hashMap.put("rsn", rsn);
			hashMap.put("rsnInt", rsnInt);
			hashMap.put("device", device);
			getSqlSession().insert("com.bwhx.eruler.mapper.RulerMapper." + "insertSN",hashMap);
		}
		
		
		return rsn;
		 
	}
	
	String rsnInt2Rsn(Integer rsnInt, String device){
		String zeroString = "";
		String rsn = "";
		if(rsnInt<=999999){
			/*for(int i=0;i<6-rsnInt.toString().length();i++){
				zeroString = "0"+zeroString;	
			}*/
			String formatSN = String.format("%06d", rsnInt);
			if("1".equals(device)){
				rsn = "AA"+formatSN;
			}else if("2".equals(device)){
				rsn = "ER"+formatSN;
			}else if("3".equals(device)){
				rsn = "EB"+formatSN;
			}else if("4".equals(device)){
				rsn = "ES"+formatSN;
			}else if("5".equals(device)){
				rsn = "EA"+formatSN;
			}
			
		}else{
			rsn="-1";
		}
		return rsn;	
	}


	@Override
	public void inputRuler(Ruler r) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "updateruler", r);
		
		
	}

	@Override
	public List<Ruler> list(Page p) {
		return getSqlSession().selectList("com.bwhx.eruler.mapper.RulerMapper." + "list",p);

	}


	@Override
	public Integer count(Ruler r) {
		Object selectOne = getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "count",r);
		if(null!=selectOne && selectOne instanceof Integer){
			return (int)selectOne;
		}
		return 0;
	}


	@Override
	public void insertChuKuDan(ChuKuDan chukudan) {
		this.getSqlSession().insert("com.bwhx.eruler.mapper.RulerMapper." + "insertchukudan", chukudan);
		
	}


	@Override
	public void update(ChuKuDan chukudan) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "updaterulerBySns", chukudan);
		
	}


	@Override
	public Ruler findByMAC(String mac) {
		Object ruler = getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findByMAC",mac);
		if(ruler==null){
			return null;
		}

		return (Ruler)ruler;
	}


	@Override
	public void updateByOrder(List<Ruler> rulers) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "updateRulerByOrder", rulers);
		
	}


	@Override
	public String findUnusedSN(String device) {
		List<Ruler> rulers = getSqlSession().selectList("com.bwhx.eruler.mapper.RulerMapper." + "findUnusedSN",device);
		if(rulers.size()==0){
			return null;
		}else{
			return rulers.get(0).getRsn();
		}
	
		
	}


	@Override
	public Boolean isRsnAvliable(String rsn) {
		Object selectOne = getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findByRSN",rsn);
		if(selectOne!=null&&((Ruler)selectOne).getrState()==2){
			return true;
		}else{
			return false;
		}
		
	}


	@Override
	public Ruler findBySn(String opcode) {
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findBySn",opcode);
	}


	@Override
	public Ruler findChuKuBySn(String opc) {
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findChuKuBySn",opc);

	}


	@Override
	public void updateByOrderBack(List<Ruler> rulers) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "updateByOrderBack", rulers);
		
	}


	@Override
	public void update(List<String> listCodes, Long time) {
		
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("listCodes", listCodes);
		map.put("time", time);
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "insertByInputStore", map);
		
	}


	@Override
	public Ruler find3BySn(String value) {
		return this.getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "find3BySn",value);
	}


	@Override
	public void insertRulerUser(Map<String, Integer> paramMap) {
		
		Object selectOne = this.getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerUserERMapper." + "findEqual",paramMap);
		if(selectOne==null){
			this.getSqlSession().insert("com.bwhx.eruler.mapper.RulerUserERMapper." + "insert", paramMap);
		}
		
	}


	@Override
	public void updateRFTime(Map map) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "updateRFTime", map);

	}


	@Override
	public void insertRulerSdk(Map<String, Object> paramMap) {
		Object selectOne = this.getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerSdkERMapper." + "findEqual",paramMap);
		if(selectOne==null){
			this.getSqlSession().insert("com.bwhx.eruler.mapper.RulerSdkERMapper." + "insert", paramMap);
		}
		
	}


	@Override
	public Ruler findByRSN(String rsn) {
		return 	(Ruler)getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findByRSN",rsn);
	}


	@Override
	public List<Ruler> list(Page p, Long startdateLong, Long enddateLong) {
		HashMap<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("p", p);
		paramMap.put("startdateLong", startdateLong);
		paramMap.put("enddateLong", enddateLong);
		
		return getSqlSession().selectList("com.bwhx.eruler.mapper.RulerMapper." + "listByQueryParam",paramMap);
	}


	@Override
	public Integer count(Ruler r, Long startdateLong, Long enddateLong) {
		
		HashMap<String, Object> paramMap = new HashMap<String,Object>();
		paramMap.put("r", r);
		paramMap.put("startdateLong", startdateLong);
		paramMap.put("enddateLong", enddateLong);
		Object selectOne = getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "countByQueryParam",paramMap);
		if(null!=selectOne && selectOne instanceof Integer){
			return (int)selectOne;
		}
		return 0;
	}


	@Override
	public Ruler findAvailableRulerBySn(String rsn) {
		
		
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findAvailableRulerBySn",rsn);
	}


	@Override
	public void baofei(List<Ruler> paramRulersList) {
		this.getSqlSession().update("com.bwhx.eruler.mapper.RulerMapper." + "baofei", paramRulersList);
	}


	@Override
	public Ruler findChuKuAndBaoFeiBySn(String opc) {
		return getSqlSession().selectOne("com.bwhx.eruler.mapper.RulerMapper." + "findChuKuAndBaoFeiBySn",opc);

	}
	
}
